/*
 * CourseAddInterFrm.java
 *
 * Created on __DATE__, __TIME__
 */

package com.jakey.view;

import java.awt.print.Book;
import java.sql.Connection;

import javax.swing.JOptionPane;

import com.jakey.dao.CourseDao;
import com.jakey.model.Course;
import com.jakey.util.DbUtil;
import com.jakey.util.StringUtil;

import Message.Message;
import client.TheClient;
import gui.DarkButton;
import gui.DarkLabel;
import gui.LightButton;
import gui.LightTextField;
import user.User;

import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.GroupLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.SystemColor;

/**
 *
 * @author  __USER__
 */
public class CourseAddInterFrm extends javax.swing.JInternalFrame {

	CourseDao coursedao=new CourseDao();

	/** Creates new form CourseAddInterFrm */
	public CourseAddInterFrm() {
		getContentPane().setBackground(SystemColor.control);
		setPreferredSize(new Dimension(700, 433));
		initComponents();
		this.setLocation(200, 50);
	}

	public CourseAddInterFrm(User user) {
		getContentPane().setBackground(SystemColor.control);
		setPreferredSize(new Dimension(700, 433));
		initComponents();
		this.setLocation(200, 50);
	}


	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new DarkLabel("");
		jLabel1.setBounds(75, 117, 65, 21);
		jLabel2 = new DarkLabel("");
		jLabel2.setBounds(334, 117, 65, 21);
		courseTimeTxt = new LightTextField();
		courseTimeTxt.setBounds(399, 117, 144, 21);
		jLabel3 = new DarkLabel("");
		jLabel3.setBounds(75, 197, 65, 21);
		jLabel4 = new DarkLabel("");
		jLabel4.setBounds(334, 198, 65, 20);
		courseNameTxt = new LightTextField();
		courseNameTxt.setBounds(140, 117, 144, 21);
		courseTeacherTxt = new LightTextField();
		courseTeacherTxt.setBounds(140, 197, 144, 21);
		capacityTxt = new LightTextField();
		capacityTxt.setBounds(399, 197, 144, 21);
		jb_add = new DarkButton("");
		jb_add.setBounds(367, 295, 65, 23);
		jb_reset = new LightButton("");
		jb_reset.setBounds(187, 295, 65, 23);

		setClosable(true);
		setIconifiable(true);
		setTitle("\u8bfe\u7a0b\u6dfb\u52a0");

		jLabel1.setText("\u8bfe\u7a0b\u540d\u79f0");

		jLabel2.setText("\u4e0a\u8bfe\u65f6\u95f4");

		jLabel3.setText("\u4efb\u8bfe\u8001\u5e08");

		jLabel4.setText("\u8bfe\u7a0b\u5bb9\u91cf");

		jb_add
				.setIcon(new javax.swing.ImageIcon(
						"D:\\Workspaces\\MyEclipse 8.5\\CourseSys\\src\\com\\jakey\\view\\image\\add.png")); // NOI18N
		jb_add.setText("\u6dfb\u52a0");
		jb_add.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jb_addActionPerformed(evt);
			}
		});

		jb_reset
				.setIcon(new javax.swing.ImageIcon(
						"D:\\Workspaces\\MyEclipse 8.5\\CourseSys\\src\\com\\jakey\\view\\image\\reset.png")); // NOI18N
		jb_reset.setText("\u91cd\u7f6e");
		jb_reset.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jb_resetActionPerformed(evt);
			}
		});
		getContentPane().setLayout(null);
		getContentPane().add(jb_reset);
		getContentPane().add(jLabel1);
		getContentPane().add(courseNameTxt);
		getContentPane().add(jLabel3);
		getContentPane().add(courseTeacherTxt);
		getContentPane().add(jLabel4);
		getContentPane().add(jLabel2);
		getContentPane().add(jb_add);
		getContentPane().add(courseTimeTxt);
		getContentPane().add(capacityTxt);

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void jb_addActionPerformed(java.awt.event.ActionEvent evt) {
		String courseName=this.courseNameTxt.getText();
		String courseTime=this.courseTimeTxt.getText();
		String courseTeacher=this.courseTeacherTxt.getText();
		String  capacity=this.capacityTxt.getText();
		
		if(StringUtil.isEmpty(courseName)){
			JOptionPane.showMessageDialog(this, "课程名称不能为空！");
			return;
		}
		if(StringUtil.isEmpty(courseTime)){
			JOptionPane.showMessageDialog(this, "上课时间不能为空！");
			return;
		}
		if(StringUtil.isEmpty(courseTeacher)){
			JOptionPane.showMessageDialog(this, "任课老师不能为空！");
			return;
		}
		if(StringUtil.isEmpty(capacity)){
			JOptionPane.showMessageDialog(this, "课程容量不能为空！");
			return;
		}
		Course course=new Course(courseName,courseTime,courseTeacher,Integer.parseInt(capacity));
		Message message = new Message("COURSE_ADD", course);
		message.setCheckCode(6);
		client.TheClient client = new TheClient();
		
		Message recevied = new Message();
		try {
			recevied = client.sendAndReceive(message);
			if(recevied.isResponse()){
				JOptionPane.showMessageDialog(this, "课程添加成功！");
				this.resetValue();
			}else{
				JOptionPane.showMessageDialog(this, "课程添加失败！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, "课程添加失败！");
		}
		
	}

	private void jb_resetActionPerformed(java.awt.event.ActionEvent evt) {
		this.resetValue();
	}
private void resetValue(){
	this.courseNameTxt.setText("");
	this.courseTeacherTxt.setText("");
	this.courseTimeTxt.setText("");
	this.capacityTxt.setText("");
}
	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private LightTextField capacityTxt;
	private LightTextField courseNameTxt;
	private LightTextField courseTeacherTxt;
	private LightTextField courseTimeTxt;
	private DarkLabel jLabel1;
	private DarkLabel jLabel2;
	private DarkLabel jLabel3;
	private DarkLabel jLabel4;
	private javax.swing.JButton jb_add;
	private javax.swing.JButton jb_reset;
	// End of variables declaration//GEN-END:variables

}